import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

class DragableWidget extends StatefulWidget {
  DragableWidget({Key? key}) : super(key: key);

  @override
  _DragableWidgetState createState() => _DragableWidgetState();
}

class _DragableWidgetState extends State<DragableWidget> {
  var _dragData;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Dragable widget')),
      body: Center(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 50,
            ),
            _buildDraggable(),
            SizedBox(
              height: 100,
            ),
            DragTarget<Color>(
              builder: (BuildContext context, List<Color?> candidateData,
                  List<dynamic> rejectedData) {
                print(
                    'candidateData:$candidateData,rejectedData:$rejectedData');
                return _dragData == null
                    ? Container(
                        height: 100,
                        width: 100,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10),
                            border: Border.all(color: Colors.red)),
                      )
                    : Container(
                        height: 100,
                        width: 100,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: Colors.red,
                            borderRadius: BorderRadius.circular(10)),
                      );
              },
              onWillAccept: (Color? color) {
                print('onWillAccept:$color');

                return true;
              },
              onAccept: (Color color) async {
                setState(() {
                  _dragData = color;
                });
                SmartDialog.showLoading(
                    displayTime: Duration(seconds: 1),
                    animationTime: Duration(milliseconds: 500));
                Future.delayed(Duration(milliseconds: 1500), () {
                  Get.back();
                });

                print('onAccept:$color');
              },
              onLeave: (Color? color) {
                setState(() {
                  _dragData = null;
                });
                print('onLeave:$color');
              },
            ),
          ],
        ),
      ),
    );
  }

  _buildDraggable() {
    return Draggable(
      axis: Axis.vertical,
      data: Colors.yellow,
      child: Container(
        height: 100,
        width: 100,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.red, borderRadius: BorderRadius.circular(10)),
      ),
      feedback: Container(
        height: 100,
        width: 100,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.red, borderRadius: BorderRadius.circular(10)),
      ),
      childWhenDragging: Container(
        height: 100,
        width: 100,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10),
        ),
      ),
    );
  }
}
